﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hfm.Core.DataAccess;
using Hfm.Core.DataAccess.CustomerStock;
using Hfm.Common.Entity.CustomerStock;
using Hfm.Common.Entity.Customer;
using Hfm.Core.DataAccess.Customer;
using Hfm.Core.DataAccess.Warehouse;
using Hfm.Core.DataAccess.ItemData;
using Eaf.Net.AccessControl.Entity.User;
using Eaf.Net.AccessControl.DataAccess.User;

namespace Hfm.Core.BusinessManager.CustomerStock
{
    public class CustomerStockManager
    {
        protected readonly CustomerStockData CustomerStockDataAccess;
        protected readonly CustomerData CustomerDataAccess;
        protected readonly WarehouseData WarehouseDataAccess;
        protected readonly ItemData ItemDataAccess;
        protected readonly UserData<UserAccountEntity> UserDataAccess;
        public CustomerStockManager()
        {

        }

        public CustomerStockManager(CustomerStockData CustomerStock, CustomerData Customer, WarehouseData Warehouse, ItemData Item,UserData<UserAccountEntity> User)
        {
            CustomerStockDataAccess = CustomerStock;
            CustomerDataAccess = Customer;
            WarehouseDataAccess = Warehouse;
            ItemDataAccess = Item;
            UserDataAccess = User;
        }

        /// <summary>
        /// 获取所有CustomerStock
        /// </summary>
        /// <returns></returns>
        public virtual IList<CustomerStockEntity> GetCustomerStocks()
        {
            return CustomerStockDataAccess.GetCustomerStocks();
        }

        /// <summary>
        /// 根据条件获取CustomerStock
        /// </summary>
        /// <param name="StockId"></param>
        /// <returns></returns>
        public virtual CustomerStockEntity GetCustomerStockByStockId(int StockId)
        {
            return CustomerStockDataAccess.getCustomerStockByStockId(StockId);
        }

        /// <summary>
        /// 根据条件获取CustomerStock
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <param name="ItemId"></param>
        /// <returns></returns>
        public virtual CustomerStockEntity GetCustomerStockByCustomerIdAndItemId(int CustomerId, int ItemId)
        {
            return CustomerStockDataAccess.GetCustomerStockByCustomerIdAndItemId(CustomerId, ItemId);
        }

        /// <summary>
        /// 根据条件获取StockInventory
        /// </summary>
        /// <param name="StockId">StockId</param>
        /// <returns></returns>
        public virtual IList<CustomerStockInventoryEntity> GetCustomerStockInventoryByStockId(int StockId)
        {
            return CustomerStockDataAccess.GetCustomerStockInventoryByStockId(StockId);
        }

        /// <summary>
        /// 获取所有StockEmailRecipient
        /// </summary>
        /// <returns></returns>
        public virtual List<CustomerStockEmailRecipientEntity> GetStockEmailRecipients()
        {
            return CustomerStockDataAccess.GetStockEmailRecipients();
        }

        /// <summary>
        /// 根据条件获取StockEmailRecipient
        /// </summary>
        /// <param name="StockId"></param>
        /// <returns></returns>
        public virtual List<CustomerStockEmailRecipientEntity> GetStockEmailRecipientByUserId(int UserId)
        {
            return CustomerStockDataAccess.GetStockEmailRecipientByUserId(UserId);
        }

        /// <summary>
        /// 根据条件获取StockSerialNumber
        /// </summary>
        /// <param name="StockId">StockId</param>
        /// <param name="SerialNumber">SerialNumber</param>
        /// <returns></returns>
        public virtual List<CustomerStockSerialNumberEntity> GetCustomerStockSerialNumberByStockId(int StockId, string SerialNumber = null)
        {
            return CustomerStockDataAccess.GetCustomerStockSerialNumberByStockId(StockId, SerialNumber);
        }

        /// <summary>
        /// 获取所有ShowCustomerStock
        /// </summary>
        /// <returns></returns>
        public virtual IList<ShowCustomerStockEntity> GetShowCustomerStocks(string CustomerName = null, string CustomerCode = null, string SerialNumber = null, string ItemCode = null)
        {
            IList<CustomerStockEntity> listCustomerStock = GetCustomerStocks();
            List<ShowCustomerStockEntity> listShowCustomerStock = new List<ShowCustomerStockEntity>();
            foreach (CustomerStockEntity CustomerStock in listCustomerStock)
            {
                List<CustomerStockSerialNumberEntity> listStockSerialNumber = new List<CustomerStockSerialNumberEntity>();
                ShowCustomerStockEntity ShowCustomerStock = new ShowCustomerStockEntity();
                NewCustomerEntity Customer = CustomerDataAccess.GetNewCustomerById(CustomerStock.CustomerId);
                if (!string.IsNullOrWhiteSpace(SerialNumber))
                {
                    listStockSerialNumber = GetCustomerStockSerialNumberByStockId(CustomerStock.Id, SerialNumber);
                }
                IList<CustomerStockInventoryEntity> listCustomerStockInventory = GetCustomerStockInventoryByStockId(CustomerStock.Id);
                int TotalQuantity = 0;
                foreach (CustomerStockInventoryEntity item in listCustomerStockInventory)
                {
                    bool bl = true;
                    if (!string.IsNullOrWhiteSpace(SerialNumber))
                        bl = false;
                    foreach (CustomerStockSerialNumberEntity StockSerialNumber in listStockSerialNumber)
                    {
                        if (StockSerialNumber.StockInventoryId == item.Id && StockSerialNumber.WarehouseId == item.WarehouseId)
                            bl = true;
                    }
                    if (bl)
                        TotalQuantity += item.Quantity;
                }
                string ShowItemCode = ItemDataAccess.GetItemById(CustomerStock.ItemId).ItemCode;

                ShowCustomerStock.Id = CustomerStock.Id;
                ShowCustomerStock.CustomerName = Customer.CustomerName;
                ShowCustomerStock.CustomerCode = Customer.CustomerCode;
                ShowCustomerStock.ItemId = CustomerStock.ItemId;
                ShowCustomerStock.ItemCode = ShowItemCode;
                ShowCustomerStock.TotalQuantity = TotalQuantity;
                listShowCustomerStock.Add(ShowCustomerStock);
            }
            if (!string.IsNullOrWhiteSpace(CustomerName))
                listShowCustomerStock = listShowCustomerStock.Where(cs => cs.CustomerName.Contains(CustomerName)).ToList();
            if (!string.IsNullOrWhiteSpace(CustomerCode))
                listShowCustomerStock = listShowCustomerStock.Where(cs => cs.CustomerCode.Contains(CustomerCode)).ToList();
            if (!string.IsNullOrWhiteSpace(ItemCode))
                listShowCustomerStock = listShowCustomerStock.Where(cs => cs.ItemCode.Contains(ItemCode)).ToList();
            if (!string.IsNullOrWhiteSpace(SerialNumber))
                listShowCustomerStock = listShowCustomerStock.Where(cs => cs.TotalQuantity > 0).ToList();
            return listShowCustomerStock;
        }

        /// <summary>
        /// 创建CustomerStock
        /// </summary>
        /// <param name="CustomerStock"></param>
        public virtual void CreateCustomerStock(CustomerStockEntity CustomerStock)
        {
            CustomerStockDataAccess.CreateCustomerStock(CustomerStock);
        }

        /// <summary>
        /// 创建StockInventory
        /// </summary>
        /// <param name="StockInventory"></param>
        public virtual void CreateStockInventory(CustomerStockInventoryEntity StockInventory)
        {
            CustomerStockDataAccess.CreateStockInventory(StockInventory);
        }

        /// <summary>
        /// 创建StockSerialNumber
        /// </summary>
        /// <param name="StockSerialNumber"></param>
        public virtual void CreateStockSerialNumber(CustomerStockSerialNumberEntity StockSerialNumber)
        {
            CustomerStockDataAccess.CreateStockSerialNumber(StockSerialNumber);
        }

        /// <summary>
        /// 创建StockEmailRecipient
        /// </summary>
        /// <param name="StockEmailRecipient"></param>
        public virtual void CreateStockEmailRecipient(CustomerStockEmailRecipientEntity StockEmailRecipient)
        {
            CustomerStockDataAccess.CreateStockEmailRecipient(StockEmailRecipient);
        }

        /// <summary>
        /// 更新CustomerStock
        /// </summary>
        /// <param name="CustomerStock"></param>
        public virtual void UpdateCustomerStock(CustomerStockEntity CustomerStock)
        {
            CustomerStockDataAccess.UpdateCustomerStock(CustomerStock);
        }

        /// <summary>
        /// 更新Stock Email Recipient
        /// </summary>
        /// <param name="StockEmailRecipient"></param>
        public virtual void UpdateStockEmailRecipient(CustomerStockEmailRecipientEntity StockEmailRecipient)
        {
            CustomerStockDataAccess.UpdateStockEmailRecipient(StockEmailRecipient);
        }

        /// <summary>
        /// 删除CustomerStock
        /// </summary>
        /// <param name="CustomerStockId"></param>
        public virtual void DeleteCustomerStock(int CustomerStockId)
        {
            CustomerStockDataAccess.DeleteCustomerStock(CustomerStockId);
        }

        /// <summary>
        /// 删除StockInventory
        /// </summary>
        /// <param name="StockId"></param>
        public virtual void DeleteStockInventory(int StockId)
        {
            CustomerStockDataAccess.DeleteStockInventory(StockId);
        }

        /// <summary>
        /// 删除StockSerialNumber
        /// </summary>
        /// <param name="StockId"></param>
        public virtual void DeletStockSerialNumber(int StockId)
        {
            CustomerStockDataAccess.DeletStockSerialNumber(StockId);
        }

        /// <summary>
        /// 删除StockEmailRecipient
        /// </summary>
        /// <param name="StockId"></param>
        public virtual void DeleteStockEmailRecipient(int StockEmailId)
        {
            CustomerStockDataAccess.DeleteStockEmailRecipient(StockEmailId);
        }

        /// <summary>
        /// 根据条件获取所有ShowStockInventory
        /// </summary>
        /// <param name="StockId"></param>
        /// <returns></returns>
        public virtual IList<ShowStockInventoryEntity> GetShowStockInventoryByStockId(int CustomerId, int ItemId)
        {
            CustomerStockEntity CustomerStock = GetCustomerStockByCustomerIdAndItemId(CustomerId, ItemId);
            List<ShowStockInventoryEntity> listShowStockInventory = new List<ShowStockInventoryEntity>();
            if (CustomerStock != null)
            {
                IList<CustomerStockInventoryEntity> listStockInventory = GetCustomerStockInventoryByStockId(CustomerStock.Id);
                foreach (CustomerStockInventoryEntity item in listStockInventory)
                {
                    ShowStockInventoryEntity ShowStockinventory = new ShowStockInventoryEntity();
                    string Warehouse = WarehouseDataAccess.GetWarehouseById(item.WarehouseId).WarehouseName;
                    ShowStockinventory.Id = item.Id;
                    ShowStockinventory.WarehouseId = item.WarehouseId;
                    ShowStockinventory.Warehouse = Warehouse;
                    ShowStockinventory.StockId = item.StockId;
                    ShowStockinventory.Quantity = item.Quantity;
                    listShowStockInventory.Add(ShowStockinventory);
                }
            }
            return listShowStockInventory;
        }

        /// <summary>
        /// 根据条件获取所有ShowStockSerialNumber
        /// </summary>
        /// <param name="StockId"></param>
        /// <returns></returns>
        public virtual IList<ShowStockSerialNumberEntity> GetShowStockSerialNumberByStockId(int CustomerId, int ItemId)
        {
            CustomerStockEntity CustomerStock = GetCustomerStockByCustomerIdAndItemId(CustomerId, ItemId);
            List<ShowStockSerialNumberEntity> listShowStockSerialNumber = new List<ShowStockSerialNumberEntity>();
            if(CustomerStock!=null)
            {
            IList<CustomerStockSerialNumberEntity> listStockInventory = GetCustomerStockSerialNumberByStockId(CustomerStock.Id);
            foreach (CustomerStockSerialNumberEntity item in listStockInventory)
            {
                ShowStockSerialNumberEntity ShowStockSerialNumber = new ShowStockSerialNumberEntity();
                string Warehouse = WarehouseDataAccess.GetWarehouseById(item.WarehouseId).WarehouseName;
                ShowStockSerialNumber.Id = item.Id;
                ShowStockSerialNumber.WarehouseId = item.WarehouseId;
                ShowStockSerialNumber.Warehouse = Warehouse;
                ShowStockSerialNumber.StockId = item.StockId;
                ShowStockSerialNumber.InStockDate = item.InStockDate;
                ShowStockSerialNumber.Pallet = item.Pallet;
                ShowStockSerialNumber.Rack = item.Rack;
                ShowStockSerialNumber.Remarks = item.Remarks;
                ShowStockSerialNumber.SerialNumber = item.SerialNumber;
                ShowStockSerialNumber.StockInventoryId = item.StockInventoryId;
                listShowStockSerialNumber.Add(ShowStockSerialNumber);
            }
                }
            return listShowStockSerialNumber;
        }

        public virtual IList<CustomerEntity> GetAllCustomer()
        {
            return CustomerDataAccess.GetAllCustomer();
        }

        public virtual List<ShowStockEmailRecipientEntity> GetShowStockEmailRecipient()
        {
            List<CustomerStockEmailRecipientEntity> listStockEmailRecipient = GetStockEmailRecipients();
            List<ShowStockEmailRecipientEntity> listShowStockEmailRecipient = new List<ShowStockEmailRecipientEntity>();
            foreach (CustomerStockEmailRecipientEntity StockEmailRecipient in listStockEmailRecipient)
            {
                ShowStockEmailRecipientEntity ShowEmailRecipient = new ShowStockEmailRecipientEntity();
                UserAccountEntity UserAccount = UserDataAccess.GetUser(StockEmailRecipient.UserId);
                ShowEmailRecipient.Id = StockEmailRecipient.Id;
                ShowEmailRecipient.StockId = StockEmailRecipient.StockId;
                ShowEmailRecipient.UserId = StockEmailRecipient.UserId;
                ShowEmailRecipient.UserName = UserAccount.Username;
                ShowEmailRecipient.FullName = UserAccount.FullName;
                ShowEmailRecipient.Email = UserAccount.Email;
                ShowEmailRecipient.Enabled = StockEmailRecipient.Enabled;
                listShowStockEmailRecipient.Add(ShowEmailRecipient);
            }
            return listShowStockEmailRecipient;
        }
    }
}
